package cn.core.kpis.rest;

import cn.core.kpis.eo.ExpressionOutput;
import cn.core.kpis.qo.ExpressionOutputQO;
import cn.core.kpis.service.ExpressionOutputService;
import cn.core.kpis.vo.ExpressionOutputVO;
import cn.core.tool.ao.DataExportAO;
import cn.core.tool.exception.CoreException;
import cn.core.tool.rest.CoreController;
import cn.core.tool.util.CoreTool;
import cn.core.tool.util.Param;
import cn.core.tool.util.Res;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 公式输出 REST API
 */
@Log4j2
@RestController
@Scope("prototype")
@RequestMapping("kpis/expressionOutput")
public class ExpressionOutputController extends CoreController {

  public static final String name = "公式输出";

  @Resource
  private ExpressionOutputService expressionOutputService;

  /**
   * 公式输出 >>> 保存
   * @param param 参数
   * @return 返回
   */
  @PostMapping(save)
  public Res<ExpressionOutput> save(@RequestBody Param<ExpressionOutput> param) throws Exception {
    Res<ExpressionOutput> res = new Res<>(name + succSave);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(expressionOutputService.save(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 公式输出 >>> 批量保存
   * @param param 参数
   * @return 返回
   */
  @PostMapping(saveBatch)
  public Res<ExpressionOutput> saveBatch(@RequestBody Param<ExpressionOutput> param) throws Exception {
    Res<ExpressionOutput> res = new Res<>(name + succSave);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(expressionOutputService.saveBatch(param.getData()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 公式输出 >>> 修改
   * @param param 参数
   * @return 返回
   */
  @PostMapping(update)
  public Res<ExpressionOutput> update(@RequestBody Param<ExpressionOutput> param) throws Exception {
    Res<ExpressionOutput> res = new Res<>(name + succUpdate);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(expressionOutputService.update(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 公式输出 >>> 批量修改
   * @param param 参数
   * @return 返回
   */
  @PostMapping(updateBatch)
  public Res<ExpressionOutput> updateBatch(@RequestBody Param<ExpressionOutput> param) throws Exception {
    Res<ExpressionOutput> res = new Res<>(name + succUpdate);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(expressionOutputService.updateBatch(param.getData()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 公式输出 >>> 删除
   * @param param 参数
   * @return 返回
   */
  @PostMapping(remove)
  public Res<ExpressionOutput> remove(@RequestBody Param<ExpressionOutput> param) throws Exception {
    Res<ExpressionOutput> res = new Res<>(name + succRemove);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      expressionOutputService.remove(param.get());
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 公式输出 >>> 批量删除
   * @param param 参数
   * @return 返回
   */
  @PostMapping(removeBatch)
  public Res<ExpressionOutput> removeBatch(@RequestBody Param<ExpressionOutput> param) throws Exception {
    Res<ExpressionOutput> res = new Res<>(name + succRemove);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      expressionOutputService.removeBatch(param.getData());
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 公式输出 >>> 数据量
   * @param param 参数
   * @return 返回
   */
  @PostMapping(count)
  public Res<ExpressionOutput> count(@RequestBody Param<ExpressionOutputQO> param) throws Exception {
    Res<ExpressionOutput> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setCount(expressionOutputService.count(null));
      } else {
        res.setCount(expressionOutputService.count(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 公式输出 >>> 主键查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(pk)
  public Res<ExpressionOutput> pk(@RequestBody Param<ExpressionOutputQO> param) throws Exception {
    Res<ExpressionOutput> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(expressionOutputService.pk(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 公式输出 >>> 查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(query)
  public Res<ExpressionOutput> query(@RequestBody Param<ExpressionOutputQO> param) throws Exception {
    Res<ExpressionOutput> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setData(expressionOutputService.query(null));
      } else {
        res.setData(expressionOutputService.query(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 公式输出 >>> 分页
   * @param param 分页参数
   * @return 返回
   */
  @PostMapping(paging)
  public Res<ExpressionOutput> paging(@RequestBody Param<ExpressionOutputQO> param) throws Exception {
    Res<ExpressionOutput> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(expressionOutputService.paging(param));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 公式输出 >>> 业务数据量
   * @param param 参数
   * @return 返回
   */
  @PostMapping(countVo)
  public Res<ExpressionOutputVO> countVo(@RequestBody Param<ExpressionOutputQO> param) throws Exception {
    Res<ExpressionOutputVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setCount(expressionOutputService.countVo(null));
      } else {
        res.setCount(expressionOutputService.countVo(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 公式输出 >>> 业务主键查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(pkVo)
  public Res<ExpressionOutputVO> pkVo(@RequestBody Param<ExpressionOutputQO> param) throws Exception {
    Res<ExpressionOutputVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get()) || CoreTool.isNullStr(param.get().getId())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(expressionOutputService.pkVo(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 公式输出 >>> 业务查询
   * @param param 参数
   * @return 返回
   */
  @PostMapping(queryVo)
  public Res<ExpressionOutputVO> queryVo(@RequestBody Param<ExpressionOutputQO> param) throws Exception {
    Res<ExpressionOutputVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        res.setData(expressionOutputService.queryVo(null));
      } else {
        res.setData(expressionOutputService.queryVo(param.get()));
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 公式输出 >>> 业务分页
   * @param param 分页参数
   * @return 返回
   */
  @PostMapping(pagingVo)
  public Res<ExpressionOutputVO> pagingVo(@RequestBody Param<ExpressionOutputQO> param) throws Exception {
    Res<ExpressionOutputVO> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(expressionOutputService.pagingVo(param));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 公式输出 >>> 数据导入
   * @param param 参数
   * @return 返回
   */
  @PostMapping(dataImport)
  public Res<ExpressionOutput> dataImport(@RequestBody Param<String> param) throws Exception {
    Res<ExpressionOutput> res = new Res<>(name + succImport);
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(expressionOutputService.dataImport(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

  /**
   * 公式输出 >>> 数据导出
   * @param param 参数
   * @return 返回
   */
  @PostMapping(dataExport)
  public Res<String> dataExport(@RequestBody Param<DataExportAO<ExpressionOutputQO>> param) throws Exception {
    Res<String> res = new Res<>();
    try {
      if (CoreTool.isNullEo(param) || CoreTool.isEmpty(param.getData()) || CoreTool.isNullEo(param.get())) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      res.setData(expressionOutputService.dataExport(param.get()));
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
    return res;
  }

}
